package com.hspedu.Reader_;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @Author Stringzhua
 * @Date 2024/3/16 19:55
 * description:使用BufferedReader读取文本文件,并显示在控制台上
 */
public class BufferedReader_ {
    public static void main(String[] args) throws IOException {
        String filePath = "e:\\qiyeke\\filecontroller\\story.java";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        //读取
        String line;//按行读取,效率高
        /*
         * 1.bufferedReader.readLine() 是按行读取文件
         * 2.当返回null,表示文件读取完毕
         * */
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        //关闭流
        //注意只需要关闭BufferedReader,因为底层会自动的取关闭 节点流
//        FileReader
        /*
         public void close() throws IOException {
                synchronized (lock) {
                    if (in == null)
                        return;
                    try {
                        in.close();//in 就是传入的 new FileReader(filePath),被关闭了
                    } finally {
                        in = null;
                        cb = null;
                    }
                }
            }
        * */
        bufferedReader.close();
    }
}
